package com.seadragon.app.ksr.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.app.ksr.dao.ReviewDao;
import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.Office;
import com.seadragon.app.ksr.model.Review;
import com.seadragon.app.ksr.service.OfficeService;
import com.seadragon.app.ksr.service.ReviewService;
import com.seadragon.app.ksr.util.IndexManager;

@Service("reviewService")
@Transactional
public class ReviewServiceImpl implements ReviewService {
	private static Logger logger = LoggerFactory.getLogger(ReviewServiceImpl.class);
	@Autowired
	private ReviewDao reviewDao;
	
	@Autowired
	private OfficeService officeService;
	
	@Autowired
	private IndexManager indexManager;
	
	@Override
	public void save(Review review){
		Office office = review.getOffice();
		office.setRating((office.getRating()*office.getReviewCount()+review.getRating())/(office.getReviewCount()+1));
		office.increaseReviewCount(1);
		officeService.update(review.getOffice());

		Review savedReview = reviewDao.save(review);
		
		try{
			indexManager.insertDocument(savedReview);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}
	}
	
	@Override
	public List<Review> findByOffice(Office office) {
		return reviewDao.findByOffice(office);
	}
	
	@Override
	public List<Review> findByOffice(Office office, int offset, int pageSize) {
		return reviewDao.findByOffice(office, offset, pageSize);
	}
	
	@Override
	public long count(Office office){
		return reviewDao.count(office);
	}

	@Override
	public List<Review> findByCustomer(Customer customer) {
		return reviewDao.findByCustomer(customer);
	}

	@Override
	public List<Review> findByCustomer(Customer customer, int offset, int pageSize) {
		return reviewDao.findByCustomer(customer, offset, pageSize);
	}

	@Override
	public List<Review> findByOfficeAndReviewRating(Office office, int reviewRating) {
		return reviewDao.findByOfficeAndReviewRating(office, reviewRating);
	}

	@Override
	public List<Review> findByOfficeAndReviewRating(Office office, int reviewRating, int offset, int pageSize) {
		return reviewDao.findByOfficeAndReviewRating(office, reviewRating, offset, pageSize);
	}

	@Override
	public List<Review> findByCustomerAndReviewRating(Customer customer, int reviewRating) {
		return reviewDao.findByCustomerAndReviewRating(customer, reviewRating);
	}

	@Override
	public List<Review> findByCustomerAndReviewRating(Customer customer, int reviewRating, int offset, int pageSize) {
		return reviewDao.findByCustomerAndReviewRating(customer, reviewRating, offset, pageSize);
	}

	@Override
	public void update(Review review) {
		Review updatedReview = reviewDao.findById(review.getId());
		Office office = updatedReview.getOffice();
		office.setRating((office.getRating()*office.getReviewCount()+review.getRating()-updatedReview.getRating())/office.getReviewCount());
		officeService.update(office);
		updatedReview.setContent(review.getContent());
		updatedReview.setRating(review.getRating());
		updatedReview.setDateUpdated(new Date());
		updatedReview.setTitle(review.getTitle());
		reviewDao.save(updatedReview);
		try{
			indexManager.insertDocument(updatedReview);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}
	}

	@Override
	public List<Review> findAll(int offset, int pageSize) {
		return reviewDao.findAll(offset, pageSize);
	}

	@Override
	public long count(Customer customer) {
		// TODO Auto-generated method stub
		return 0;
	}
}
